package com.demo.controller;

import com.demo.dto.DepartmentDTO;
import com.demo.entity.SysUser;
import com.demo.model.JsonMessage;
import com.demo.security.AuthUserDetail;
import com.demo.service.SysUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@Api(value = "用户信息接口")
@RequestMapping("/admin/sysUser")
public class SysUserController {
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private SysUserService userService;


    //按部门查询
    @ApiOperation(value = "部门查询")
    @PostMapping("listDept")
    @ResponseBody
    public JsonMessage listDept(@RequestParam(defaultValue = "0") int pageIndex,
                                @RequestParam(defaultValue = "10") int pageSize,
                                String depId) {
        JsonMessage message = new JsonMessage();
        PageInfo<SysUser> page = userService.listDep(pageIndex, pageSize, depId);
        message.setData(page);
        return message;
    }

    @ApiOperation(value = "按部门查询成员")
    @PostMapping("listDeptUser")
    @ResponseBody
    public JsonMessage listDeptUser(String depId) {
        JsonMessage message = new JsonMessage();
        List<SysUser> list = userService.listDepUser(depId);
        message.setData(list);
        return message;
    }

    @ApiOperation(value = "员工列表")
    @PostMapping("list")
    @ResponseBody
    public JsonMessage list(@RequestParam(defaultValue = "0") int pageIndex,
                            @RequestParam(defaultValue = "10") int pageSize) {
        JsonMessage message = new JsonMessage();
        PageInfo<SysUser> page = userService.list(pageIndex, pageSize);
        message.setData(page);
        return message;
    }

    @ApiOperation(value = "添加员工")
    @PostMapping("add")
    @ResponseBody
    public JsonMessage addSysUser(@RequestBody SysUser user) {
        JsonMessage message = new JsonMessage();
        if (userService.confirmLoginName(user.getLoginName(), user.getTelephone(), user.getDingDing()) == 0) {
            user.setUserId(UUID.randomUUID().toString());
            user.setCreateTime(new Date());
            user.setDisabled(false);
            userService.add(user);
        } else {
            message.setData(0);
        }
        return message;
    }

    @ApiOperation(value = "更新员工信息")
    @PostMapping("update")
    @ResponseBody
    public JsonMessage updateSysUser(@RequestBody SysUser user) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        userService.update(user);
        return message;
    }

    @ApiOperation(value = "员工密码")
    @PostMapping("password")
    @ResponseBody
    public JsonMessage password(Authentication authentication,
                                String loginName, String loginPass, String passOld) {
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        JsonMessage message = new JsonMessage();
        String pass = userService.pass(loginName);
        boolean f = passwordEncoder.matches(passOld, pass);
        if (f == true) {
            userService.password(loginName, loginPass);
            message.setData("更新成功");
        } else {
            message.setData("旧密码错误");
        }

        return message;
    }

    @ApiOperation(value = "密码重置")
    @PostMapping("passwordReset")
    @ResponseBody
    public JsonMessage passwordReset(String loginName, String loginPass) {
        JsonMessage message = new JsonMessage();
        userService.password(loginName, loginPass);
        message.setData("重置成功");
        return message;
    }

    @ApiIgnore
    @PostMapping("backUser")
    @ResponseBody
    public JsonMessage backUser(String userId) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        userService.backUser(userId);
        return message;
    }

    @ApiOperation(value = "获取登录名")
    @PostMapping("getByLoginName")
    @ResponseBody
    public JsonMessage getByLoginName(String userId) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        SysUser sysUser = userService.getByLoginName(userId);
        message.setData(sysUser);
        return message;
    }

    @ApiOperation(value = "获取个人所有信息")
    @PostMapping("getByUserId")
    @ResponseBody
    public JsonMessage getByUserId(String userId) {
        JsonMessage message = new JsonMessage("更新成功", 200, true);
        SysUser sysUser = userService.getByUserId(userId);
        message.setData(sysUser);
        return message;
    }

    @ApiOperation(value = "删除个人信息")
    @PostMapping("remove")
    @ResponseBody
    public JsonMessage remove(String loginName) {
        JsonMessage message = new JsonMessage("删除成功", 200, true);
        userService.remove(loginName);
        return message;
    }


    //根据userId查询用户所有信息
    @ApiOperation(value = "根据userId查询用户所有信息")
    @PostMapping("findFromUserID")
    @ResponseBody
    public JsonMessage findFromUserID(Authentication authentication) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        SysUser sysUser = userService.findFromUserID(detail.getUserId());
        message.setData(sysUser);
        return message;
    }

    @ApiOperation(value = "查询部门员工")
    @PostMapping("findDepartmentUser")
    @ResponseBody
    public JsonMessage findDepartmentUser() {
        JsonMessage message = new JsonMessage();
        List<DepartmentDTO> dtoList = userService.findDepartmentUserList();
        for (DepartmentDTO dto : dtoList
        ) {
            for (SysUser user : dto.getUserList()
            ) {
                user.setDepId(user.getUserId());
                user.setDepName(user.getTrueName());
            }
        }
        message.setData(dtoList);
        return message;
    }

    @ApiOperation(value = "角色更改")
    @PostMapping("roleUpdate")
    @ResponseBody
    public JsonMessage roleUpdate(Authentication authentication, String userId, String roleId) {
        JsonMessage message = new JsonMessage();
        AuthUserDetail detail = (AuthUserDetail) authentication.getDetails();
        if (detail.getUserId().equals(userId)) {
            userService.roleId(userId, roleId);
            message.setData("self");
        } else {
            userService.roleId(userId, roleId);
            message.setData("更改成功");
        }

        return message;
    }


    @ApiOperation(value = "查询所有审核人员")
    @PostMapping("findAllLeader")
    @ResponseBody
    public JsonMessage findAllLeader() {
        JsonMessage message = new JsonMessage();
        List<SysUser> list = userService.findAllLeader();
        message.setData(list);
        return message;
    }
}
